﻿using Dapper;
using System;
using BCEntity.TMS.OrderPackingRecord;
using System.Collections.Generic;
using System.Data;
using System.Text;
using System.Threading.Tasks;
using BCCommon;

namespace BCData.TMS.OrderPackingRecord
{
	/// <summary>
	/// 订单包装材料记录
	/// </summary>
	public class OrderPackingRecordData : IOrderPackingRecordData
	{
		private readonly IDatabaseContext databaseContext;
		private const string QUERY_EXISTS = @"SELECT 1 FROM orderpackingrecord";
		private const string QUERY = @"SELECT OrderPackingRecordId,DimensionUnit,ShowUnit,TransformRatio,Price,CreateTime,OrderId,OrderNo,PackingConfigId,
											PackingCategoryConfigId,PackingTitle,PackingWidth,PackingLength,PackingHeight 
										FROM orderpackingrecord";
		public OrderPackingRecordData(IDatabaseContext databaseContext)
		{
			this.databaseContext = databaseContext;
		}
		#region 检查订单包装材料记录是否存在
		public async Task<bool> Exists(long orderPackingRecordId)
		{
			string sql = string.Format(@"{0} WHERE OrderPackingRecordId=?OrderPackingRecordId", QUERY_EXISTS);
			DynamicParameters parameters = new DynamicParameters();
			parameters.Add("?OrderPackingRecordId", orderPackingRecordId, DbType.Int64);
			return await this.databaseContext.Master.ExecuteScalarAsync(sql, parameters) != null;
		}
		#endregion

		#region 添加订单包装材料记录
		public async Task<bool> Add(List<OrderPackingRecordEntity> entities)
		{
			return await this.databaseContext.Master.InsertAllAsync(entities, databaseContext.Transaction) > 0;
		}

		public async Task<OrderPackingRecordEntity> Add(OrderPackingRecordEntity entity)
		{
			string sql = SqlCommands.GetInsertSql("orderpackingrecord", new string[] { "DimensionUnit", "ShowUnit", "TransformRatio", "Price", "OrderId", "OrderNo",
				"PackingConfigId", "PackingCategoryConfigId", "PackingTitle", "PackingWidth", "PackingLength", "PackingHeight" });
			sql = string.Format("{0} {1} WHERE OrderPackingRecordId=LAST_INSERT_ID();", sql, QUERY);
			DynamicParameters parameters = new DynamicParameters();
			parameters.Add("?DimensionUnit", entity.DimensionUnit?.Trim(), DbType.AnsiString, size: 10);
			parameters.Add("?ShowUnit", entity.ShowUnit?.Trim(), DbType.AnsiString, size: 10);
			parameters.Add("?TransformRatio", entity.TransformRatio, DbType.Int32, size: 11);
			parameters.Add("?Price", entity.Price, DbType.Decimal, size: 10);
			parameters.Add("?OrderId", entity.OrderId, DbType.Int64, size: 20);
			parameters.Add("?OrderNo", entity.OrderNo?.Trim(), DbType.AnsiString, size: 50);
			parameters.Add("?PackingConfigId", entity.PackingConfigId, DbType.Int32, size: 11);
			parameters.Add("?PackingCategoryConfigId", entity.PackingCategoryConfigId, DbType.Int32, size: 11);
			parameters.Add("?PackingTitle", entity.PackingTitle?.Trim(), DbType.AnsiString, size: 50);
			parameters.Add("?PackingWidth", entity.PackingWidth, DbType.Int32, size: 11);
			parameters.Add("?PackingLength", entity.PackingLength, DbType.Int32, size: 11);
			parameters.Add("?PackingHeight", entity.PackingHeight, DbType.Int32, size: 11);
			return await this.databaseContext.Master.QueryFirstOrDefaultAsync<OrderPackingRecordEntity>(sql, parameters, databaseContext.Transaction);

		}
		#endregion

		#region 修改订单包装材料记录
		public async Task<OrderPackingRecordEntity> Update(OrderPackingRecordEntity entity)
		{
			string sql = SqlCommands.GetUpdateSql("orderpackingrecord", new string[] { "DimensionUnit", "ShowUnit", "TransformRatio", "Price", "OrderId", "OrderNo",
				"PackingConfigId", "PackingCategoryConfigId", "PackingTitle", "PackingWidth", "PackingLength", "PackingHeight" }, new string[] { "OrderPackingRecordId" });
			sql = string.Format("{0} {1} WHERE OrderPackingRecordId=?OrderPackingRecordId;", sql, QUERY);
			DynamicParameters parameters = new DynamicParameters();
			parameters.Add("?OrderPackingRecordId", entity.OrderPackingRecordId, DbType.Int64, size: 20);
			parameters.Add("?DimensionUnit", entity.DimensionUnit?.Trim(), DbType.AnsiString, size: 10);
			parameters.Add("?ShowUnit", entity.ShowUnit?.Trim(), DbType.AnsiString, size: 10);
			parameters.Add("?TransformRatio", entity.TransformRatio, DbType.Int32, size: 11);
			parameters.Add("?Price", entity.Price, DbType.Decimal, size: 10);
			parameters.Add("?OrderId", entity.OrderId, DbType.Int64, size: 20);
			parameters.Add("?OrderNo", entity.OrderNo?.Trim(), DbType.AnsiString, size: 50);
			parameters.Add("?PackingConfigId", entity.PackingConfigId, DbType.Int32, size: 11);
			parameters.Add("?PackingCategoryConfigId", entity.PackingCategoryConfigId, DbType.Int32, size: 11);
			parameters.Add("?PackingTitle", entity.PackingTitle?.Trim(), DbType.AnsiString, size: 50);
			parameters.Add("?PackingWidth", entity.PackingWidth, DbType.Int32, size: 11);
			parameters.Add("?PackingLength", entity.PackingLength, DbType.Int32, size: 11);
			parameters.Add("?PackingHeight", entity.PackingHeight, DbType.Int32, size: 11);
			return await this.databaseContext.Master.QueryFirstOrDefaultAsync<OrderPackingRecordEntity>(sql, parameters, databaseContext.Transaction);
		}
		#endregion

		#region 删除订单包装材料记录
		public async Task<bool> Delete(long orderPackingRecordId)
		{
			string sql = @"DELETE FROM orderpackingrecord WHERE OrderPackingRecordId=?OrderPackingRecordId";
			DynamicParameters parameters = new DynamicParameters();
			parameters.Add("?OrderPackingRecordId", orderPackingRecordId, DbType.Int64);
			return await this.databaseContext.Master.ExecuteAsync(sql, parameters, databaseContext.Transaction) > 0;
		}
		#endregion

		#region 获取订单包装材料记录
		public async Task<OrderPackingRecordEntity> GetEntity(long orderPackingRecordId)
		{
			string sql = string.Format(@"{0} WHERE OrderPackingRecordId=?OrderPackingRecordId", QUERY);
			DynamicParameters parameters = new DynamicParameters();
			parameters.Add("?OrderPackingRecordId", orderPackingRecordId, DbType.Int64);
			return await this.databaseContext.Master.QueryFirstOrDefaultAsync<OrderPackingRecordEntity>(sql, parameters);
		}
		#endregion

		#region 获取订单包装材料记录列表
		public async Task<IEnumerable<OrderPackingRecordEntity>> GetList(string orderNo)
		{
			string sql = string.Format("{0} WHERE OrderNo=?OrderNo", QUERY);
			DynamicParameters parameters = new DynamicParameters();
			parameters.Add("OrderNo", orderNo, DbType.AnsiString);
			return await this.databaseContext.Master.QueryAsync<OrderPackingRecordEntity>(sql, parameters);
		}
		public async Task<IEnumerable<OrderPackingRecordEntity>> GetList(long orderId)
		{
			string sql = string.Format("{0} WHERE OrderId=?OrderId", QUERY);
			DynamicParameters parameters = new DynamicParameters();
			parameters.Add("OrderId", orderId, DbType.Int64);
			return await this.databaseContext.Master.QueryAsync<OrderPackingRecordEntity>(sql, parameters);
		}
		#endregion

		#region 获取订单包装材料记录分页
		public async Task<(IEnumerable<OrderPackingRecordEntity>, int)> Query(DateTime? start, DateTime? end, string sortName, bool? ascending, int pageIndex, int pageSize)
		{
			List<string> wheres = new List<string>();
			DynamicParameters parameters = new DynamicParameters();

			if (start.HasValue && !end.HasValue)
			{
				wheres.Add("CreateTime>=?Start");
				parameters.Add("?Start", start.Value, DbType.DateTime);
			}
			else if (!start.HasValue && end.HasValue)
			{
				wheres.Add("CreateTime<=?End");
				parameters.Add("?End", end.Value, DbType.DateTime);
			}
			else if (start.HasValue && end.HasValue)
			{
				wheres.Add("CreateTime>=?Start AND CreateTime<=?End");
				parameters.Add("?Start", start.Value, DbType.DateTime);
				parameters.Add("?End", end.Value, DbType.DateTime);
			}
			string sql = string.Format(@"{0} {1} ORDER BY {4} {5} LIMIT {2},{3}; SELECT COUNT(1) FROM orderpackingrecord {1}",
				  QUERY,
				  wheres.Count > 0 ? " WHERE " + string.Join(" AND ", wheres) : "",
				  (pageIndex == 0 ? 0 : pageIndex - 1) * pageSize,
				  pageSize,
				  string.IsNullOrWhiteSpace(sortName) ? "CreateTime" : sortName,
				  ascending.HasValue && ascending.Value ? "ASC" : "DESC");
			return await this.databaseContext.QueryMultipleAsync<OrderPackingRecordEntity, int>(sql, parameters);
		}
        #endregion
    }
}



